package action;

import gate.ActionHandler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.user.SessionUser;

import dm.LoggerDM;
import dm.UserDM;


public class ActionUser implements ActionHandler{
	
	@Override
	public void handleAction(HttpServletRequest req, HttpServletResponse res) {

		try {
			UserDM dm = new UserDM();
			HttpSession session = req.getSession();
			String url = req.getParameter("URL");
			String action = req.getParameter("action");
			
			if(action.equals("CREATE_USER")){
				
				try {
					String username = req.getParameter("username");
					String password = req.getParameter("password");
					String password2 = req.getParameter("password2");
					String name = req.getParameter("name");
					String tel = req.getParameter("tel");
					String mail = req.getParameter("mail");
					
					SessionUser user = dm.createUser(username, password, password2, name, tel, mail);
					session = req.getSession();
					session.setAttribute("user",user);
					
				} catch (Exception e) {
					req.setAttribute("error", "Error !"+e.getMessage());
				}
				
			}else if(action.equals("DEACTIVATE_USER")){
				
				try {
					String username = req.getParameter("username");
					String password = req.getParameter("password");

					dm.deactivateUser(username, password);
				} catch (Exception e) {
					req.setAttribute("error", "Error !"+e.getMessage());
				}
				
			}else if(action.equals("UPDATE_USER")){
				try {
					SessionUser user = (SessionUser) session.getAttribute("user");
					String username = req.getParameter("username");
					String newPass = req.getParameter("password");
					String name = req.getParameter("name");
					String tel = req.getParameter("tel");
					String mail = req.getParameter("mail");
					
					dm.updateUser(user, username, newPass, name, tel, mail);
				} catch (Exception e) {
					req.setAttribute("error", "Error !"+e.getMessage());
				}
				
			}else if(action.equals("CHANGE_PASS")){
				try {
					String username = req.getParameter("username");
					String password = req.getParameter("password");
					String newPassword = req.getParameter("newpassword");

					dm.changePassword(username, password, newPassword);
				} catch (Exception e) {
					req.setAttribute("error", "Error !"+e.getMessage());
				}
				
			}else if(action.equals("CHECK_USER")){
				
				try {
					String username = req.getParameter("username");
					String password = req.getParameter("password");
					SessionUser user = dm.checkUser(username,password);
					session = req.getSession();
					session.setAttribute("user",user);
					
				} catch (Exception e) {
					req.setAttribute("error", "Error !"+e.getMessage());
				}
			}else if(action.equals("LOGOUT")){
				try {
					SessionUser user = (SessionUser)session.getAttribute("user");
					LoggerDM ldm = new LoggerDM();
					ldm.logoutLog(user.getUserId());
					
					session.removeAttribute("user");
					
				} catch (Exception e) {
					req.setAttribute("error", "Error !"+e.getMessage());
				}
			}else{
				req.setAttribute("error", "Action not found !");
			}
			
			req.getRequestDispatcher(url).forward(req, res);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

}

